<?php

/*
 *
 |--------------------------------------------------------------------------
 | Application Routes
 |--------------------------------------------------------------------------
 |
 | Here is where you can register all of the routes for an application.
 | It's a breeze. Simply tell Laravel the URIs it should respond to
 | and give it the controller to call when that URI is requested.
 |
 */
Route::get('/', function () {
    exit(json_encode(array('code' => 500, 'message' => '请求错误', 'data' => "")));
});

//这里自定义解析接口的路由
Route::get('price/{method}', function ($method) {
    //Log::error('error',array('context'=>'additional info'));
    $controller =  'price';
    $rs = null;
    try {
        //过滤用户输入
        filter();
        $className = 'App\Http\Controllers\\' . ucfirst($controller) . 'Controller';
        if (class_exists($className)) {
            $obj = new $className;
            if (method_exists($obj, $method)) {
                $rs = $obj->$method();
                if (!is_string($rs) && !is_array($rs)) {
                    $rs = "";
                }
                $rs = json_encode(array('code' => 0, 'message' => 'ok', 'data' => $rs),JSON_UNESCAPED_UNICODE);
            }
        }
        if (is_null($rs)) {
            throw new App\Exceptions\SystemException(500);
        }
    } catch (App\Exceptions\SystemException $e) {
        $rs = json_encode(array('code' => $e->getCode(), 'message' => $e->getMessage(), 'data' => ""),JSON_UNESCAPED_UNICODE);
        $code = intval($e->getCode());
        if ($code < 300){
            Log::info('info', array($e->getCode(), $e->getMessage()));
        }else if( $code < 400){
            Log::warning('warning', array($e->getCode(), $e->getMessage()));
        }else {
            Log::error('error', array($e->getCode(), $e->getMessage()));
        }
    } catch (\Exception $e){
        $rs = json_encode(array('code' => $e->getCode(), 'message' => $e->getMessage(), 'data' => ""),JSON_UNESCAPED_UNICODE);
        Log::error('error', array($e->getCode(), $e->getMessage()));
    }

    $rs = empty($rs) ? "" : $rs;
    exit($rs);
});

